﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;

namespace zingget
{
    public partial class frmZingGet : Form
    {
        public string messageContent;
        public frmZingGet()
        {
            InitializeComponent();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            string url = txtLink.Text.ToString();
            int errorCode = checkLink(url);
            messageContent = "";
            DialogResult message;

            txtLink.Focus();
            txtLink.SelectAll();

            switch (errorCode)
            {
                case 0:
                    string result = "http://mp3.zing.vn/download/vip/song/" + get320URL(url);
                    bool sysType = System.Environment.Is64BitOperatingSystem;
                    
                    if (sysType.Equals(true))
                    {
                        //Windows 64bit
                        Process.Start("C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe", "/d " + result);
                    } 
                    else
                    {
                        //Windows 32bit
                        Process.Start("C:\\Program Files\\Internet Download Manager\\IDMan.exe", "/d " + result);
                    }
                    break;
                case 1:
                    message = MessageBox.Show("Chưa nhập link bài hát!", "", MessageBoxButtons.OK);
                    break;
                case 2:
                    message = MessageBox.Show("Link sai!", "", MessageBoxButtons.OK);
                    break;
                case 3:
                    message = MessageBox.Show("Link sai!", "", MessageBoxButtons.OK);
                    break;
                case 4:
                    message = MessageBox.Show("Chỉ hỗ trợ cho mp3.zing.vn", "", MessageBoxButtons.OK);
                    break;
                case 5:
                    message = MessageBox.Show("Chỉ hỗ trợ link của 1 bài hát.\n\nVí dụ: http://mp3.zing.vn/bai-hat/Goi-Mua-Trung-Quan-Idol/ZW6CD0B9.html", "", MessageBoxButtons.OK);
                    break;
            }
        }
        //http://mp3.zing.vn/bai-hat/Goi-Mua-Trung-Quan-Idol/ZW6CD0B9.html
        private int checkLink(string url)
        {
            int errorCode;
            string zing_domain = "http://mp3.zing.vn/";
            string isHTML = "";
            string linkType = "";
            string domain = "";

            if (url.Length == 0) 
            {
                errorCode = 1;
            } 
            else if (url.Length <= 5 || url.Length <= 19)
            {
                errorCode = 2; //Link sai dinh dang: duoi 5 ki tu
            }
            else
            {
                linkType = url.Substring(19, 1);
                isHTML = url.Substring(url.Length - 5, 5);
                domain = url.Substring(0, 19);

                if (isHTML.ToLower().Equals(".html").Equals(false))
                {
                    errorCode = 3; //Link sai dinh dang: format khong phai la .html
                }
                else if (domain.Equals(zing_domain).Equals(false))
                {
                    errorCode = 4; //Link khong phai cua zing
                }
                else if (linkType.Equals("a"))
                {
                    errorCode = 5; //Link cua album
                }
                else 
                { 
                    errorCode = 0; //Link dung
                } 
            }
            
            return errorCode;
        }

        public string get320URL(string url)
        {
            string specifySong = url.Substring(url.Length - 13, 8);
            return specifySong;
        }
    }
}